import wx_message.receive as receive
import wx_message.reply as reply
from wx_message.reply import ArticleParameters
from static_replying import *

from command_handler import CommandHandler

last_msg_id = ''

class __MsgHandler__():
    def __init__(self, receive_message : receive.TextMsg | receive.ImageMsg | receive.EventMsg | receive.VoiceMsg) -> None:
        self.receive_message = receive_message


    def __replyText__(self, content : str) -> reply.TextMsg:
        return reply.TextMsg(
            self.receive_message.FromUserName,
            self.receive_message.ToUserName,
            content
        )


    def __replyImage__(self, media_id : str) -> reply.ImageMsg:
        return reply.ImageMsg(
            self.receive_message.FromUserName,
            self.receive_message.ToUserName,
            media_id
        )


    def __replyArticles__(self, article_list : list[ArticleParameters]) -> reply.ArticleMsg:
        return reply.ArticleMsg(
            self.receive_message.FromUserName,
            self.receive_message.ToUserName,
            article_list
        )


    def ReplyMessage(self) -> reply.TextMsg | reply.ImageMsg | reply.ArticleMsg:
        return self.__replyText__(DEFAULT_REPLY)


class __TextMsgHandler__(__MsgHandler__):
    def __init__(self, receive_message: receive.TextMsg) -> None:
        self.receive_message = receive_message


    def ReplyMessage(self):
        if CommandHandler.IsCommand(self.receive_message.Content):
            return self.__replyText__(CommandHandler.Return(self.receive_message.Content))

        return super().ReplyMessage()


class __ImageMsgHandler__(__MsgHandler__):
    def __init__(self, receive_message: receive.ImageMsg) -> None:
        self.receive_message = receive_message


class __EventMsgHandler__(__MsgHandler__):
    def __init__(self, receive_message: receive.EventMsg) -> None:
        self.receive_message = receive_message


class __VoiceMsgHandler__(__MsgHandler__):
    def __init__(self, receive_message: receive.VoiceMsg) -> None:
        self.receive_message = receive_message


    def ReplyMessage(self) -> reply.TextMsg:
        if self.receive_message.Recognition is None:
            return self.__replyText__('没有识别到内容')

        if '小爱同学' in self.receive_message.Recognition:
            content = '我在'
        else:
            content = '识别结果 :\n' + self.receive_message.Recognition

        return self.__replyText__(content)


class __DuplicateMsgHandler__(__MsgHandler__):
    def ReplyMessage(self) -> reply.TextMsg:
        return self.__replyText__(busy_reply)


def HandleMessage(receive_message : receive.TextMsg | receive.ImageMsg | receive.EventMsg | receive.VoiceMsg) -> str:
    global last_msg_id
    if receive_message.MsgId == last_msg_id:
        return __DuplicateMsgHandler__(receive_message).ReplyMessage().BuildMsg()

    last_msg_id = receive_message.MsgId
    if isinstance(receive_message, receive.TextMsg):
        handler = __TextMsgHandler__(receive_message)
    elif isinstance(receive_message, receive.ImageMsg):
        handler = __ImageMsgHandler__(receive_message)
    elif isinstance(receive_message, receive.EventMsg):
        handler = __EventMsgHandler__(receive_message)
    elif isinstance(receive_message, receive.VoiceMsg):
        handler = __VoiceMsgHandler__(receive_message)

    reply_message = handler.ReplyMessage()
    return reply_message.BuildMsg()